<template>
    <div>
        <el-form :model="literature" :rules="rules" label-position="right" ref="literatureFrom" label-width="100px" :inline='true'>
                <div el-row>
                    <el-form-item label="文献名称" prop="docName" >
                        <el-input v-model="literature.docName" el-col :span="8"></el-input>
                    </el-form-item>
                    <el-form-item label="文献类型" prop="docType" el-col :span="12">
                        <el-input v-model="literature.docType"></el-input>
                    </el-form-item>
                </div>
                <div el-row>
                    <el-form-item label="发表刊物"  el-col :span="8" prop="publications" >
                        <el-input v-model="literature.publications"></el-input>
                    </el-form-item>
                    <el-form-item label="发表时间" prop="publishTime" el-col :span="8">
                        <el-date-picker
                            v-model="literature.publishTime"
                            type="datetime"
                            format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择发表时间">
                        </el-date-picker>
                    </el-form-item>
                </div>
                <div el-row>
                    <el-form-item label="发表人" prop="publisher" el-col :span="8">
                        <el-input v-model="literature.publisher"></el-input>
                    </el-form-item>
                    <el-form-item label="发表人职位"  el-col :span="8" prop="publications" >
                        <el-input v-model="literature.position"></el-input>
                    </el-form-item>
                </div>
                <el-form-item label="影响因子"  el-col :span="8" prop="publications" >
                     <el-input-number v-model="literature.level" :precision="1" :step="0.1" :max="99.9"></el-input-number>
                </el-form-item>
                <div el-row v-if="activeName==='9'">
                    <el-form-item label="原文献">
                        <el-upload action="/api/adscm/pebase/peDocument/insert" class="upload-demo" style="width: 99%;margin: 0 auto;" ref="uploadEnclosure"
                              :auto-upload="false" :limit="1" :http-request="uploadSectionFile">
                            <el-button  slot="trigger" size="small" type="primary">选择文献</el-button>
                        </el-upload>
                    </el-form-item>
                </div>
                <el-form-item>
                    <el-button type="primary" @click="addUpdate()">{{addType=='insert'?'新增':'修改'}}</el-button>
                </el-form-item>
        </el-form>
    </div>
</template>
<script>
import {quillEditor} from 'vue-quill-editor';
import api from '@/axios/api.js'
export default {
    name:'literature',
    props:{
        id:Number,
        activeName:String,
        addType:String,
        literature:Object
    },
    data(){
        return {
            rules:{
                docName:[{required: true, message: '请输入文献名称', trigger: 'blur' }],
                publisher:[{required: true, message: '请输入发表人', trigger: 'blur' }],
                publishTime:[{required: true, message: '请输入发表时间', trigger: 'blur' }]
            }
        }
    },
    watch:{
        detail(newDetail,oldDetail){
            let self =this;
        }
    },
    methods:{
        addUpdate(){
            let self = this;
            if(self.activeName==='9'){
                this.$refs['literatureFrom'].validate((valid) => {
                    if(valid){
                        if(self.$refs.uploadEnclosure.uploadFiles.length>0){
                            self.$refs.uploadEnclosure.submit();
                        }else{
                            self.$message.error(`请选择原文献`);
                        }

                    }
                })
            }
        },
        uploadSectionFile(param){
        let self = this;
        let fileObj = param.file;
        let form =new FormData();
        let peDocument={
            did:self.id,
            docName:self.literature.docName,
            docType:self.literature.docType,
            publications:self.literature.publications,
            publisher:self.literature.publisher,
            publishTime:self.timestampToTime(self.literature.publishTime),
            position:self.literature.position
        }
        self.literature.level?peDocument.level=self.literature.level:0;
        form.append('peDocument',JSON.stringify(peDocument));
        form.append('file',fileObj);
        api.peDocument(form).then(res=>{
            self.$message.success(`原文献新增成功`);
            self.$emit('addHandle',self.activeName,true);
        },error=>{
            self.$message.error(`原文献新增失败`);
                self.$emit('addHandle',self.activeName,true);
        })
    }
    }
}
</script>
<style>

</style>

